﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SMS.Stocks
{
    public partial class NewStock : System.Web.UI.Page
    {
        //New Mode            = 0 , Update Mode =1
        private bool IsUpdate = false;
        SMS.DAL.Stock obj = null;
        SMS.Business.Stocks.StocksBusiness stockBus;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");

            stockBus = new Business.Stocks.StocksBusiness();

            if (Request.QueryString["StockID"] != null)
            {
                IsUpdate = true;
                if (!IsPostBack)
                {
                    obj = stockBus.GetByID(int.Parse(SMS.Helper.QueryEncoder.Decrypt(Request.QueryString["StockID"])));
                    txtName.Text = obj.Name;
                    txtMobile.Text = obj.Mobile;
                    txtAdress.Text = obj.Address;
                    drpStockType.Items.FindByValue(obj.Type.ToString()).Selected = true;
                    btnSave.Text = "تحديث";
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsUpdate)
            {
                stockBus.Insert(new DAL.Stock()
                {
                    Type = drpStockType.SelectedIndex,
                    Name = txtName.Text,
                    Mobile = txtMobile.Text.ToString(),
                    Address = txtAdress.Text
                });
                if (drpStockType.SelectedIndex == 0)
                {
                    lblResult.Text = "تم ادخل المحل بنجاح";
                }
                else
                {
                    lblResult.Text = "تم ادخل المخزن بنجاح";
                }
            }
            else
            {
                obj = new DAL.Stock();
                obj.ID = int.Parse(SMS.Helper.QueryEncoder.Decrypt(Request.QueryString["StockID"]));
                obj.Name = txtName.Text;
                obj.Mobile = txtMobile.Text;
                obj.Address = txtAdress.Text;
                obj.Type = int.Parse(drpStockType.SelectedItem.Value.ToString());
                stockBus.Update(obj);
                if (drpStockType.SelectedIndex == 0)
                {
                    lblResult.Text = "تم تحديث المحل بنجاح";

                }
                else
                {
                    lblResult.Text = "تم تحديث المخزن بنجاح";

                }
            }

        }

    }
}